package com.guo.redis.business.hello.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.listener.ChannelTopic;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 基于 Redis 的 PubSub 实现订单异步处理之 下订单部分
 */
@RestController
@RequestMapping("/pubsub")
@Slf4j
public class PubSubController {

    @Autowired
    private ChannelTopic channelTopic;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    // sub/pub 模型， 这里 生产一条消息
    @RequestMapping("/publish")
    public String publish(String val) {
        stringRedisTemplate.convertAndSend(channelTopic.getTopic(), val);
        log.info("send to topic {} one order : {} success!", channelTopic.getTopic(), val);
        return "success";
    }
}
